<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "600px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
      console.log(this.chartData)
    },
    setOptions(chartData) {
      // console.log(lineTitle);
      // console.log(lineData);
      this.chart.clear();
      this.chart.setOption({
        title:{
          // text: '各商品分类商品数量占比',
          left:'center'
        },
        tooltip: {
          trigger: "item",
          formatter: "{a}<br/>{b}:{c} ({d}%)"
        },
        legend: {
          bottom: 10,
          left: "center",
          data: this.opinion
        },
        series: [
          {
            name: "商品分类",
            type: "pie",
            radius: "65%",
            center: ["50%", "50%"],
            avoidLabelOverlap: false,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              },
              color: function(params) {
                //自定义颜色
                var colorList = ['#c23531','#2f4554','#61a0a8','#d48265',
                                 '#91c7ae','#749f83','#ca8622','#bda29a',
                                 '#6e7074','#546570','#c4ccd3','#ffb6c1',
                                 '#c23531','#2f4554','#61a0a8','#d48265',
                                 '#91c7ae','#749f83','#ca8622','#bda29a',
                                 '#6e7074','#546570','#c4ccd3','#ffb6c1',];
                return colorList[params.dataIndex];
              }
            },
            data: chartData
          }
        ]
        
      });
    }
  }
};
</script>
